package com.tarena.lbs.business.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.service.BusinessService;

import com.tarena.lbs.common.exception.ServiceException;
import com.tarena.lbs.common.response.JsonResult;
import com.tarena.lbs.common.response.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理和商家相关的接口功能
 */
@Slf4j
@RestController
@Tag(name = "商家相关功能接口")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    //商家的分页列表查询
    //读数据入参 单个属性 id name 对象数据 query结尾
    @Operation(summary = "查询商家列表")
    @GetMapping("/admin/basic/business/info/list")
    public JsonResult<PageResult<BusinessVO>> pageList(BusinessQuery query) throws ServiceException {
        PageResult<BusinessVO> voPages= businessService.selectList(query);

        return  JsonResult.ok();
    }

    //表单填写完毕 新增提交商家数据保存
    //form请求表单格式参数 key=value&key=value
    //请求体携带json {"key","value"}
    //lbs智慧营销项目中 写操作 如果多个属性 命名类型Param后缀
    @Operation(summary = "新增商家")
    @PostMapping("/admin/basic/business/info/save")
    public JsonResult<Integer> save(@RequestBody @Validated BusinessParam param) throws ServiceException {
        log.info("控制层保存信息:{}", param);
        try {
            Integer id = businessService.save(param);
            return JsonResult.ok(id);
        } catch (ServiceException e) {
            return JsonResult.error(e.getCode(), e.getMessage());
        }catch (Exception e){
            return JsonResult.error(StatusCode.UNKNOWN_ERROR.getCode(), "系统异常，请联系管理员");
        }
    }

    //商家的业务中 活动查询商家 和旗下店铺列表封装
    @Operation(summary = "商家详情")
    @GetMapping("/basic/business/info/detail/{id}")
    public JsonResult<BusinessStoreVO> busiStoreDetail(@PathVariable("id") Integer businessId) throws ServiceException {
        log.info("控制层查询商家详情,接收到参数商户id:{}", businessId);
        BusinessStoreVO bvo=businessService.getBusinessById(businessId);
        //测试
        log.info("控制层查询商家详情,返回结果:{}", bvo);
        return JsonResult.ok(bvo);
    }

}
